/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package DLSched.Plugins;

import java.io.BufferedReader;
import java.io.DataInputStream;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.logging.Level;
import java.util.logging.Logger;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import org.apache.commons.configuration.ConfigurationException;
import org.apache.commons.configuration.HierarchicalINIConfiguration;

/**
 *
 * @author HP
 */
public class YoutubeChannel implements Hoster
{

    /**
     * Gets youtube links from a user's channel.
     * Will get only the amount of links defined by the depth key
     * in the config file.
     * @param filepath String filepath of the html to parse for links
     * @return ArrayList<String> of found links
     */
    @Override
    public ArrayList<String> get_links()
    {
        ArrayList<String> possible_links = new ArrayList();
        try
        {
            HierarchicalINIConfiguration config =
                    new HierarchicalINIConfiguration("config.ini");
            FileInputStream fstream = null;
            try
            {
                fstream = new FileInputStream(config.getString(
                                                "download.filepath"));
                DataInputStream in = new DataInputStream(fstream);
                BufferedReader br = new BufferedReader(
                                                    new InputStreamReader(in));
                String line_to_search; //Line to be scanned

                /* This is the regex pattern we're looking for */
                Pattern p = Pattern.compile(".+(\"/watch\\?v=.+\")+.+(src).+");
                try
                {
                    while ((line_to_search = br.readLine()) != null &&
                            possible_links.size() <
                            config.getInt("YoutubeChannel.depth"))
                    {
                        Matcher m = p.matcher(line_to_search);
                        if (m.matches())
                        {
                            /* Lets make sure we don't have duplicate links */
                            if (!line_to_search.contains("div"))
                            {
                                line_to_search = line_to_search.substring(
                                        line_to_search.indexOf("/watch?v="),
                                        line_to_search.indexOf(" class=") - 1);
                                possible_links.add("http://www.youtube.com" 
                                        + line_to_search);
                            }
                        }
                    }
                    for (int i = 0; i < possible_links.size(); i++)
                        Logger.getLogger(YoutubeChannel.class.getName()).
                            log(Level.INFO, "Link Added: {0}",
                            possible_links.get(i));
                }
                catch (IOException ex)
                {
                    Logger.getLogger(YoutubeChannel.class.getName()).
                            log(Level.SEVERE, "IOException caught", ex);
                }
            }
            catch (FileNotFoundException ex)
            {
                Logger.getLogger(YoutubeChannel.class.getName()).
                        log(Level.SEVERE,
                        "Exception caught opening FileInputStream", ex);
            }
        }
        catch (ConfigurationException ex)
        {
            Logger.getLogger(YoutubeChannel.class.getName()).log(Level.SEVERE,
                    "Exception caught in opening config file", ex);
        }
        return possible_links;
    }
}
